import { MessageBox } from 'mint-ui';
let mutations = {
    changeNum(state) {
        state.num++
    },
    changeSeckillListDatas(state, data) {
        //data指的是额外数据--也就是ajax请求出来的数据
        state.seckillListDatas = data
    },
    changeHomeListDatas(state, data) {
        state.homeListDatas = state.homeListDatas.concat(data)
    },
    changeHomeCateListDatas(state, data) {  //首页分类
        state.homeCateListDatas = data
    },
    changeHomeCoodsListDatas(state, data) { //首页分类列表
        state.homeCoodsListDatas = state.homeCoodsListDatas.concat(data)
    },
    changeCatId(state, data) {
        state.catId = data
    },
    //修改分类左侧数据的方法
    changeCategoryLeftDatas(state, data) {
        state.categoryLeftDatas = data
    },
    //修改分类右侧数据的方法
    changeCategoryRightDatas(state, data) {
        state.categoryRightDatas = data
    },
    changeCateAd(state, data) {
        state.cateAd = data
    },
    changeIsLoading(state, data) {
        state.isLoading = data
    },
    changeGoodsDetailDatas(state, data) {


        state.goodsDetailDatas = data
    },
    changeCartDatas(state, data) {  //添加购物车
        if (data) {
            state.cartDatas.unshift(data)
        }
        console.log(data);
        localStorage.setItem("carts", JSON.stringify(state.cartDatas))
    },
    //点击商品前边的选择框
    changeSelect(state, data) {
        console.log(state);
        console.log(data); //{cart: {…}, index: 0}
        console.log(state.cartDatas[data.index]);
        state.cartDatas[data.index] = data.cart

        //遍历购物车数据，判断每一个数据是否选中，如果都选中了isSelectAll=true
        let isSelectAll = state.cartDatas.every((item) => {
            return item.isSelect == true
        })
        state.selectAll = isSelectAll

        //更新本地存储
        localStorage.setItem("carts", JSON.stringify(state.cartDatas))
        localStorage.setItem("selectAll", JSON.stringify(state.selectAll))
    },
    //点击全选按钮
    changeSelectAllFn(state) {
        state.selectAll = !state.selectAll
        //遍历购物车数据让每一项都选中或者取消选中
        state.cartDatas.forEach(item => {
            item.isSelect = state.selectAll
        })
        localStorage.setItem("carts", JSON.stringify(state.cartDatas))
        localStorage.setItem("selectAll", JSON.stringify(state.selectAll))
    },
    //点击+
    add(state, index) {
        state.cartDatas[index].value++
        localStorage.setItem("carts", JSON.stringify(state.cartDatas))
    },
    //点击-
    jian(state, index) {
        if (state.cartDatas[index].value <= 1) {
            state.cartDatas[index].value = 1
        } else {
            state.cartDatas[index].value--
        }
        localStorage.setItem("carts", JSON.stringify(state.cartDatas))
    },
    dele(state, index) {
        MessageBox.confirm('亲，您确定要删除该宝贝吗?').then(() => {
            console.log("点击了确定按钮");
            state.cartDatas.splice(index, 1)
            localStorage.setItem("carts", JSON.stringify(state.cartDatas))
        }, () => {
            console.log("点击了取消按钮");

        });
    },
    //生成海报
    changePosterDatas(state, data) {
        state.posterDatas = data
    },
    changeIsLogin(state, data) {
        state.isLogin = data
    }
}

export default mutations